package com.xindong.paking.config;

import com.xindong.paking.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
@Slf4j
public class WebMvcConfig extends WebMvcConfigurationSupport{

    // 拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        log.info("加载了用户权限拦截器");
        // 放行的路径
        String passUrls[] = {
                "/loginAdmin",              // 管理员登陆模块接口
                "/user/add",                 // 添加新用户接口
                "/swagger-resources/**",    // swagger
                "/webjars/**",
                "/v2/**",
                "/swagger-ui.html/**",
                "/role/add",                // 权限模块接口
                "/role/delete",
                "/role/list",
                "/message/**",              // 信息模块接口
                "/login/**",                // 登录模块接口
                "/personal/**",             // 测试模块接口
                "/parking/parking-lot/**",  // 停车场管理模块接口
                "/space/**",
                "/parking/coupon/**",       // 优惠券管理模块接口
                "/paking/file/upload",      // 文件上传接口
//                "/tb-balance/**",           // 余额模块接口
//                "/parting/tb-order/**",      // 订单模块接口
                "/space/list/**",
                "/parking/carousel-chart/**"  //轮播图模块接口
        };

        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns(passUrls);
    }

    // 静态资源加载
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("静态资源加载");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

    }

    // 解决跨域
    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOriginPatterns("*") // 支持域
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE", "OPTIONS"}) // 支持方法
                .allowedHeaders("*")
                .exposedHeaders("*");
    }
}
